package com.unknow.first.dapp.admin.account.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unknow.first.dapp.admin.account.mapper.AddressCoinConfig;
import com.unknow.first.dapp.admin.account.service.AddressCoinConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.cloud.annotation.AuthLog;
import org.cloud.annotation.SystemResource;
import org.cloud.constant.CoreConstant;
import org.cloud.utils.CollectionUtil;
import org.cloud.vo.CommonApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/coin/config")
@Slf4j
@SystemResource(path = "/admin/coin/config")
@Api(value = "管理员：币种配置相关接口", tags = "管理员：币种配置相关接口")
public class AdminCoinController {

    @Autowired
    AddressCoinConfigService addressCoinConfigService;


    @SystemResource(value = "/coinList", description = "管理员后台查询币种配置列表", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.GET, value = "/coinList")
    @ApiOperation("管理员后台查询币种配置列表")
    public CommonApiResult<List<AddressCoinConfig>> coinList(Integer status) {
        QueryWrapper<AddressCoinConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(status != null, AddressCoinConfig::getStatus, status);
        List<AddressCoinConfig> configList = addressCoinConfigService.getBaseMapper().selectList(queryWrapper);
        return CommonApiResult.createSuccessResult(configList);
    }

    @SystemResource(value = "/updateCoin", description = "管理员后台更新币种信息", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/updateCoin")
    @AuthLog(bizType = "AdminCoin.updateCoin", desc = "更新币种信息")
    @ApiOperation("管理员后台更新币种信息")
    public CommonApiResult<Integer> updateCoin(@RequestBody AddressCoinConfig addressCoinConfig) {
        QueryWrapper<AddressCoinConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AddressCoinConfig::getCoinId, addressCoinConfig.getCoinId());

        AddressCoinConfig addressCoinConfigUpdate = new AddressCoinConfig();
        addressCoinConfigUpdate.setCoinId(addressCoinConfig.getCoinId());
        if (CollectionUtil.single().isNotEmpty(addressCoinConfig.getStatus())) {
            addressCoinConfigUpdate.setStatus(addressCoinConfig.getStatus());
        }

        if (CollectionUtil.single().isNotEmpty(addressCoinConfig.getTransferStatus())) {
            addressCoinConfigUpdate.setTransferStatus(addressCoinConfig.getTransferStatus());
        }

        if (CollectionUtil.single().isNotEmpty(addressCoinConfig.getWithdrawCharge())) {
            addressCoinConfigUpdate.setWithdrawCharge(addressCoinConfig.getWithdrawCharge());
        }

        if (CollectionUtil.single().isNotEmpty(addressCoinConfig.getChargeCoinId())) {
            addressCoinConfigUpdate.setChargeCoinId(addressCoinConfig.getChargeCoinId());
        }

        if (CollectionUtil.single().isNotEmpty(addressCoinConfig.getWithdrawAuthLimit())) {
            addressCoinConfigUpdate.setWithdrawAuthLimit(addressCoinConfig.getWithdrawAuthLimit());
        }
        if (CollectionUtil.single().isNotEmpty(addressCoinConfig.getWithdrawPerDayLimit())) {
            addressCoinConfigUpdate.setWithdrawPerDayLimit(addressCoinConfig.getWithdrawPerDayLimit());
        }

        if (CollectionUtil.single().isNotEmpty(addressCoinConfig.getWithdrawChargeType())) {
            addressCoinConfigUpdate.setWithdrawChargeType(addressCoinConfig.getWithdrawChargeType());
        }

        return CommonApiResult.createSuccessResult(addressCoinConfigService.getBaseMapper().update(addressCoinConfigUpdate, queryWrapper));
    }
}
